package com.zyl.blog.interceptor;/**
 * @author 14323
 * @date 2020/8/8 13:28
 */


import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @Description: TODO
 * @author: scott
 * @date: 2020年08月08日 13:28
 */
public class LoginInterceptor extends HandlerInterceptorAdapter {

    //登录拦截
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {

        //如果Session中没有User对象（即没有登录的情况下），就重定向到登录页面
        if (request.getSession().getAttribute("user") == null) {
            response.sendRedirect("/admin");
            return false;
        }

        //否则就放行
        return true;
    }
}
